home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d16 / wsmooth.arc / WSMSRC.ARC / FILE.C next >
C/C++ Source or Header  |  1990-10-09  |  17KB  |  488 lines

  1. // file.c RHS functions for the File pulldown menu selections
  2.  
  3. #include<windows.h>
  4. #include<stdio.h>
  5. #include"wsmooth.h"
  6. #include"wsmooth2.h"
  7. #include"filebuf.h"
  8. #include<string.h>
  9.  
  10. #define WM_WSM_DLGPAINT (WM_USER+0x100)
  11.  
  12. extern HWND WinSmooth;
  13. HANDLE hDlgBrush;
  14.  
  15. void InitSettings(HWND hDlg);
  16. void ProcessHScroll(WORD wParam, LONG lParam);
  17. BOOL ValidateSRate(HWND hDlg);
  18. BOOL ValidatePixRows(HWND hDlg);
  19.  
  20. void od_UpdateListBox(HWND hDlg);
  21. void od_ChangeDfExt(PSTR Ext, PSTR Name);
  22. void od_SeparateFilePath(HWND hDlg, LPSTR lpDestPath,
  23.       LPSTR lpDestFileName, LPSTR lpSrcFileName);
  24. void od_AddExtension(PSTR Name, PSTR Ext);
  25.  
  26. // About Box manager function
  27. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  28.     {
  29.     switch (message)
  30.         {
  31.         case WM_INITDIALOG:
  32.             return (TRUE);
  33.  
  34.         case WM_COMMAND:
  35.             if (wParam == IDOK || wParam == IDCANCEL)
  36.                 {
  37.                 EndDialog(hDlg, TRUE);
  38.                 return (TRUE);
  39.                 }
  40.             break;
  41.         }
  42.     return (FALSE);
  43.     }
  44.  
  45. extern HANDLE hOrgBrush;
  46. extern SETTINGS settings;
  47.  
  48. static SETTINGS s;
  49. static HWND hSrateScroll,hPixRowsScroll,hRedScroll,hGreenScroll,hBlueScroll;
  50. static HDC hDC;
  51. static short fRed = 0, fGreen = 0, fBlue = 0, bRed = 0xff, bGreen = 0xff,
  52.    bBlue = 0xff;
  53. static unsigned foreground = 1;
  54. static char text[50];
  55.  
  56. BOOL FAR PASCAL Settings(HWND hDlg, unsigned message, WORD wParam,
  57.    LONG lParam)
  58.     {
  59.     switch (message)
  60.         {
  61.         case WM_INITDIALOG:
  62.             InitSettings(hDlg);
  63.             return (TRUE);
  64.  
  65.         case WM_PAINT:                  // if dialog is painted
  66.             PostMessage(hDlg, WM_WSM_DLGPAINT, 0, 0L);
  67.             return FALSE;
  68.  
  69.         case WM_WSM_DLGPAINT:
  70.             SetBkMode(hDC,OPAQUE);      // has to be OPAQUE for background color to show
  71.             SetTextColor(hDC, RGB(fRed,fGreen,fBlue));
  72.             SetBkColor(hDC, RGB(bRed,bGreen,bBlue));
  73.  
  74.             TextOut(hDC,0,0,(LPSTR)text,strlen(text));
  75.             return TRUE;
  76.  
  77.         case WM_VSCROLL:                // handle changes to 2 scroll bars
  78.             {
  79.             HWND hsb;
  80.             char temp[10];
  81.  
  82.             if(hsb = HIWORD(lParam))
  83.                 {
  84.                 if(hsb == hSrateScroll)   // handle changes to rate scroll
  85.                     {
  86.                     if(wParam == SB_LINEDOWN && settings.Msecs > MINMSECS)
  87.                         settings.Msecs--;
  88.                     else if(wParam == SB_LINEUP && settings.Msecs < MAXMSECS)
  89.                         settings.Msecs++;
  90.                     sprintf(temp,"%4u",settings.Msecs);
  91.                     SetDlgItemText(hDlg, IDC_EB_SRATE, temp);
  92.                     }
  93.                 if(hsb == hPixRowsScroll) // handle changes to row scroll
  94.                     {
  95.                     if(wParam == SB_LINEDOWN &&
  96.                            settings.PixelRows > MINPIXELROWS)
  97.                         settings.PixelRows--;
  98.                     else if(wParam == SB_LINEUP &&
  99.                            settings.PixelRows < MAXPIXELROWS)
  100.                         settings.PixelRows++;
  101.                     sprintf(temp,"%1u",settings.PixelRows);
  102.                     SetDlgItemText(hDlg, IDC_EB_PIXROWS, temp);
  103.                     }
  104.                 }
  105.             }
  106.             return TRUE;
  107.  
  108.         case WM_HSCROLL:
  109.             ProcessHScroll(wParam, lParam);
  110.             return TRUE;
  111.  
  112.         case WM_COMMAND:
  113.             switch(wParam)
  114.                 {
  115.                 case IDC_RB_FG:         // foreground/background switch
  116.                 case IDC_RB_BG:
  117.                     foreground = (wParam == IDC_RB_FG) ? TRUE : FALSE;
  118.                     SendMessage(GetDlgItem(hDlg,IDC_RB_FG),BM_SETCHECK,
  119.                         foreground,0L);
  120.                     SendMessage(GetDlgItem(hDlg,IDC_RB_BG),BM_SETCHECK,
  121.                         !foreground,0L);
  122.                     SetScrollPos(hRedScroll,SB_CTL,(foreground ? fRed : bRed),
  123.                         TRUE);
  124.                     SetScrollPos(hGreenScroll,SB_CTL,
  125.                         (foreground ? fGreen : bGreen),TRUE);
  126.                     SetScrollPos(hBlueScroll,SB_CTL,
  127.                         (foreground ? fBlue : bBlue),TRUE);
  128.                     break;
  129.  
  130.                 case IDC_CB_STRIP:
  131.                     settings.stripbits = !settings.stripbits;
  132.                     break;
  133.  
  134.                 case IDC_BU_SAVE:         // save settings
  135.                     {
  136.                     char buf[20];
  137.  
  138.                     if(!ValidateSRate(hDlg))
  139.                         break;
  140.                     if(!ValidatePixRows(hDlg))
  141.                         break;
  142.                     SendMessage(WinSmooth,WM_WSM_HOURGLASS_CURSOR,0,0L);
  143.                     settings.back = RGB(bRed,bGreen,bBlue);
  144.                     settings.fore = RGB(fRed,fGreen,fBlue);
  145.  
  146.                     sprintf(buf,"%d",settings.Msecs);
  147.                     WriteProfileString((LPSTR)"WinSmooth",(LPSTR)"Msecs",(LPSTR)buf);
  148.                     sprintf(buf,"%d",settings.PixelRows);
  149.                     WriteProfileString((LPSTR)"WinSmooth",(LPSTR)"PixelRows",(LPSTR)buf);
  150.                     sprintf(buf,"%lu",settings.back);
  151.                     WriteProfileString((LPSTR)"WinSmooth",(LPSTR)"BackGround",(LPSTR)buf);
  152.                     sprintf(buf,"%lu",settings.fore);
  153.                     WriteProfileString((LPSTR)"WinSmooth",(LPSTR)"ForeGround",(LPSTR)buf);
  154.                     sprintf(buf,"%d",settings.stripbits);
  155.                     WriteProfileString((LPSTR)"WinSmooth",(LPSTR)"StripBits",(LPSTR)buf);
  156.  
  157.                     SendMessage(WinSmooth,WM_WSM_NORMAL_CURSOR,0,0L);
  158.                     }
  159.                     break;
  160.  
  161.                 case IDCANCEL:
  162.                     settings = s;       // structure assign to restore settings
  163.                 case IDOK:              // save current settings
  164.                     if(wParam == IDOK)  // save to WIN.INI
  165.                         {
  166.                         HDC h;
  167.  
  168.                         if(!ValidateSRate(hDlg))
  169.                             break;
  170.                         if(!ValidatePixRows(hDlg))
  171.                             break;
  172.                         h = GetDC(WinSmooth);
  173.                         settings.back = RGB(bRed,bGreen,bBlue);
  174.                         settings.fore = RGB(fRed,fGreen,fBlue);
  175.                         ResetBackGround(h);
  176.                         settings.stripbits = (SendMessage(
  177.                                  GetDlgItem(hDlg,IDC_CB_STRIP),
  178.                                  BM_GETCHECK, 0, 0L) ? TRUE: FALSE);
  179.                         filebuf_strip(settings.stripbits);
  180.                         ReleaseDC(WinSmooth,h);
  181.                         InvalidateRect(WinSmooth,NULL,TRUE);
  182.                         }
  183.                         // code common to OK and CANCEL
  184.                     DeleteObject(hDlgBrush);
  185.                     ReleaseDC(hDlg, hDC);
  186.                     EndDialog(hDlg, TRUE);
  187.                     return (TRUE);
  188.                 default:
  189.                     break;
  190.                 }
  191.             break;
  192.         }
  193.     return (FALSE);
  194.     }
  195.  
  196.    // initialize Settings dialog
  197. void InitSettings(HWND hDlg)
  198.     {
  199.     HWND hwnd;
  200.     char temp[10];
  201.  
  202.     s = settings;               // structure assign to save settings
  203.                                 // initialize the edit boxes
  204.     sprintf(temp,"%4u",settings.Msecs);
  205.     SetDlgItemText(hDlg, IDC_EB_SRATE, temp);
  206.     sprintf(temp,"%1u",settings.PixelRows);
  207.     SetDlgItemText(hDlg, IDC_EB_PIXROWS, temp);
  208.                                 // get the horz scroll handles
  209.     hSrateScroll = GetDlgItem(hDlg, IDC_SB_SRATE);
  210.     hPixRowsScroll = GetDlgItem(hDlg, IDC_SB_PIXROWS);
  211.     hRedScroll = GetDlgItem(hDlg, IDC_SB_RED);
  212.     hGreenScroll = GetDlgItem(hDlg, IDC_SB_GREEN);
  213.     hBlueScroll = GetDlgItem(hDlg, IDC_SB_BLUE);
  214.                                 // set the scroll bar ranges
  215.     SetScrollRange(hSrateScroll,SB_CTL,MINMSECS,MAXMSECS,TRUE);
  216.     SetScrollRange(hPixRowsScroll,SB_CTL,MINPIXELROWS,MAXPIXELROWS,TRUE);
  217.     SetScrollRange(hRedScroll,SB_CTL,0,255,TRUE);
  218.     SetScrollRange(hGreenScroll,SB_CTL,0,255,TRUE);
  219.     SetScrollRange(hBlueScroll,SB_CTL,0,255,TRUE);
  220.                                 // and color scroll bar positions
  221.     fRed = GetRValue(settings.fore);
  222.     fGreen = GetGValue(settings.fore);
  223.     fBlue = GetBValue(settings.fore);
  224.     bRed = GetRValue(settings.back);
  225.     bGreen = GetGValue(settings.back);
  226.     bBlue = GetBValue(settings.back);
  227.  
  228.     SetScrollPos(hRedScroll,SB_CTL,(foreground ? fRed : bRed),TRUE);
  229.     SetScrollPos(hGreenScroll,SB_CTL,(foreground ? fGreen : bGreen),TRUE);
  230.     SetScrollPos(hBlueScroll,SB_CTL,(foreground ? fBlue : bBlue),TRUE);
  231.                                 // and color of sample text
  232.     hDC = GetDC(hwnd = GetDlgItem(hDlg,IDC_EB_COLOR));
  233.     GetDlgItemText(hDlg,IDC_EB_COLOR,(LPSTR)text,sizeof(text)-1);
  234.  
  235.     {
  236.     HANDLE hOldBrush;
  237.  
  238.     hDlgBrush = CreateSolidBrush(settings.back);
  239.     hOldBrush = SelectObject(hDC,hDlgBrush);
  240.     DeleteObject(hOldBrush);
  241.     SetClassWord(hwnd,GCW_HBRBACKGROUND,hDlgBrush);
  242.     }
  243.  
  244.     SetBkMode(hDC,OPAQUE);      // has to be OPAQUE for background color to show
  245.                                 // check/uncheck the check box
  246.     SendMessage(GetDlgItem(hDlg,IDC_CB_STRIP),BM_SETCHECK,settings.stripbits,0L);
  247.                                 // check the foreground radio button
  248.     SendMessage(GetDlgItem(hDlg,IDC_RB_FG),BM_SETCHECK,foreground,0L);
  249.                                 // uncheck the background
  250.     SendMessage(GetDlgItem(hDlg,IDC_RB_BG),BM_SETCHECK,!foreground,0L);
  251.     
  252.                                 // limit scrolling rate box to 4 chars
  253.     SendMessage(GetDlgItem(hDlg,IDC_EB_SRATE),EM_LIMITTEXT,4,0L);
  254.                                 // limit pixel rows box to 1 char
  255.     SendMessage(GetDlgItem(hDlg,IDC_EB_PIXROWS),EM_LIMITTEXT,1,0L);
  256.     }
  257.  
  258.    // processes color-setting horizontal scroll bars
  259. void ProcessHScroll(WORD wParam, LONG lParam)
  260.     {
  261.     HWND hsb;
  262.     short *color;
  263.  
  264.     hsb = HIWORD(lParam);
  265.     if(hsb == hRedScroll)
  266.         color = (foreground ? &fRed : &bRed);
  267.     if(hsb == hGreenScroll)
  268.         color = (foreground ? &fGreen : &bGreen);
  269.     if(hsb == hBlueScroll)
  270.         color = (foreground ? &fBlue : &bBlue);
  271.  
  272.     switch(wParam)
  273.         {
  274.         case SB_PAGEDOWN:
  275.             *color += 15;       // fall thru
  276.         case SB_LINEDOWN:
  277.             *color = min(255,*color+1);
  278.             break;
  279.         case SB_PAGEUP:
  280.             *color -= 15;       // fall thru
  281.         case SB_LINEUP:
  282.             *color = max(0, *color-1);
  283.             break;
  284.         case SB_TOP:
  285.             *color = 0;
  286.             break;
  287.         case SB_BOTTOM:
  288.             *color = 255;
  289.             break;
  290.         case SB_THUMBPOSITION:
  291.         case SB_THUMBTRACK:
  292.             *color = LOWORD(lParam);
  293.         default:
  294.             break;
  295.         }
  296.     SetScrollPos(hsb,SB_CTL,*color,TRUE);
  297.     SetBkMode(hDC,OPAQUE);      // has to be OPAQUE for background color to show
  298.     SetTextColor(hDC, RGB(fRed,fGreen,fBlue));
  299.     SetBkColor(hDC, RGB(bRed,bGreen,bBlue));
  300.  
  301.     TextOut(hDC,0,0,(LPSTR)text,strlen(text));
  302.     }
  303.  
  304.  
  305. BOOL ValidateSRate(HWND hDlg)
  306.     {
  307.     int newrate;
  308.     BOOL flag;
  309.     
  310.     newrate = GetDlgItemInt(hDlg, IDC_EB_SRATE,(BOOL far *)&flag,0);
  311.     if(!flag || (newrate < MINMSECS) || (newrate > MAXMSECS))
  312.         {
  313.         Message(WinSmooth,"Scrolling rate must be a value between %u and %u.",
  314.             MINMSECS,MAXMSECS);
  315.         SendDlgItemMessage(hDlg, IDC_EB_SRATE, EM_UNDO, 0, 0L);
  316.         return FALSE;
  317.         }
  318.     settings.Msecs = newrate;
  319.     return TRUE;
  320.     }
  321.  
  322. BOOL ValidatePixRows(HWND hDlg)
  323.     {
  324.     int newrate;
  325.     BOOL flag;
  326.     
  327.     newrate = GetDlgItemInt(hDlg, IDC_EB_PIXROWS,(BOOL far *)&flag,0);
  328.     if(!flag || (newrate < MINPIXELROWS) || (newrate > MAXPIXELROWS))
  329.         {
  330.         Message(WinSmooth,"Number of pixel rows scrolled must be a value between %u and %u.",
  331.             MINPIXELROWS,MAXPIXELROWS);
  332.         SendDlgItemMessage(hDlg, IDC_EB_PIXROWS, EM_UNDO, 0, 0L);
  333.         return FALSE;
  334.         }
  335.     settings.PixelRows = newrate;
  336.     return TRUE;
  337.     }
  338.  
  339.  
  340.  
  341. char od_filename[128];
  342. char od_openname[128];
  343. char od_defpath[128];
  344. char od_defspec[13] = "*.txt";
  345. char od_default_extension[] = ".txt";
  346. char str[255];
  347.  
  348.    // lets user select and open files
  349. HANDLE FAR PASCAL OpenDlg(HWND hDlg, unsigned message, WORD wParam,
  350.    LONG lParam)
  351.     {
  352.     BOOL result;
  353.  
  354.     switch (message)
  355.         {
  356.         case WM_INITDIALOG:                        // message: initialize    
  357.             od_UpdateListBox(hDlg);
  358.             SetDlgItemText(hDlg, IDC_EDIT, od_defspec);
  359.             SendDlgItemMessage(hDlg,IDC_EDIT,EM_SETSEL,NULL,
  360.                MAKELONG(0, 0x7fff));
  361.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  362.             return (FALSE); // Indicates the focus is set to a control 
  363.             break;
  364.  
  365.         case WM_COMMAND:
  366.             switch (wParam)
  367.                 {
  368.                 case IDC_LISTBOX:
  369.                     switch (HIWORD(lParam))
  370.                         {
  371.                         case LBN_SELCHANGE:
  372.                             //  If item is a directory name, append "*.*" 
  373.                             if (DlgDirSelect(hDlg, str, IDC_LISTBOX))
  374.                                 strcat(str, od_defspec);
  375.  
  376.                             SetDlgItemText(hDlg, IDC_EDIT, str);
  377.                             SendDlgItemMessage(hDlg,IDC_EDIT,EM_SETSEL,NULL,MAKELONG(0, 0x7fff));
  378.                             break;
  379.  
  380.                         case LBN_DBLCLK:
  381.                             goto openfile;
  382.                         }
  383.                     return (TRUE);
  384.                     break;
  385.  
  386.                 case IDOK:
  387. openfile:
  388.                     GetDlgItemText(hDlg, IDC_EDIT, od_openname, 128);
  389.                     if (strchr(od_openname, '*') || strchr(od_openname, '?'))
  390.                         {
  391.                         od_SeparateFilePath(hDlg, (LPSTR) str, (LPSTR) od_defspec,
  392.                             (LPSTR) od_openname);
  393.                         if (str[0])
  394.                             strcpy(od_defpath, str);
  395.                         od_ChangeDfExt(od_default_extension, od_defspec);
  396.                         od_UpdateListBox(hDlg);
  397.                         return (TRUE);
  398.                         }
  399.  
  400.                     if (!od_openname[0])
  401.                         {
  402.                         MessageBox(hDlg, "No filename specified.",
  403.                             NULL, MB_OK | MB_ICONHAND);
  404.                         return (TRUE);
  405.                         }
  406.  
  407.                     od_AddExtension(od_openname, od_default_extension);
  408.                         // open the file
  409.                     result = WSopenfile(od_openname);
  410.                     strcpy(od_filename, od_openname);
  411.                     EndDialog(hDlg, result);
  412.                     return (TRUE);
  413.  
  414.                 case IDCANCEL:
  415.                     EndDialog(hDlg, FALSE);
  416.                     return (TRUE);
  417.                 }
  418.         }
  419.     return FALSE;
  420.     }
  421.  
  422.    // updates the list box in the Open Dialog
  423. void od_UpdateListBox(HWND hDlg)
  424.     {
  425.     strcpy(str, od_defpath);
  426.     strcat(str, od_defspec);
  427.     DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  428.  
  429.         // list is for subdir of current drive directory
  430.     if (!strchr (od_defpath, ':'))
  431.         DlgDirList(hDlg, od_defspec, IDC_LISTBOX, IDC_PATH, 0x4010);
  432.  
  433.         // remove the '..', if one
  434.     if (strstr (od_defpath, ".."))
  435.         od_defpath[0] = '\0';
  436.     SetDlgItemText(hDlg, IDC_EDIT, od_defspec);
  437.     }
  438.  
  439.    // changes default extension
  440. void od_ChangeDfExt(PSTR Ext, PSTR Name)
  441.     {
  442.     PSTR pTptr;
  443.  
  444.     pTptr = Name;
  445.     while (*pTptr && *pTptr != '.')
  446.         pTptr++;
  447.     if (*pTptr)
  448.         if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  449.             strcpy(Ext, pTptr);
  450.     }
  451.  
  452.    // separates file and path names
  453. void od_SeparateFilePath(HWND hDlg, LPSTR lpDestPath,
  454.       LPSTR lpDestFileName, LPSTR lpSrcFileName)
  455.     {
  456.     LPSTR lpTmp;
  457.     char  cTmp;
  458.  
  459.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  460.     while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  461.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  462.     if (*lpTmp != ':' && *lpTmp != '\\') {
  463.         lstrcpy(lpDestFileName, lpSrcFileName);
  464.         lpDestPath[0] = 0;
  465.         return;
  466.     }
  467.     lstrcpy(lpDestFileName, lpTmp + 1);
  468.     cTmp = *(lpTmp + 1);
  469.     lstrcpy(lpDestPath, lpSrcFileName);
  470.      *(lpTmp + 1) = cTmp;
  471.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  472.     }
  473.  
  474.    // adds default extension
  475. void od_AddExtension(PSTR Name, PSTR Ext)
  476.     {
  477.     PSTR pTptr;
  478.  
  479.     pTptr = Name;
  480.     while (*pTptr && *pTptr != '.')
  481.         pTptr++;
  482.     if (*pTptr != '.')
  483.         strcat(Name, Ext);
  484.     }
  485.  
  486.  
  487.  
  488.